package com.napenalki.toto.domain;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;

import com.napenalki.toto.validator.Group;

public class Team implements Comparable<Team> {

    private String name;
    private File flag;
    private String group;
    private List<String> playersList;
    private List<Match> groupMatches;

    private int points = 0;
    private int goalsFor = 0;
    private int goalsAgainst = 0;

    public Team() {
        this(null, null, null, null);
    }

    public Team(String name, File flag, String group, List<String> playersList) {
        this(name, flag, group, playersList, new ArrayList<Match>(0));
    }

    public Team(String name, File flag, String group, List<String> playersList,
            List<Match> groupMatches) {
        this.name = name;
        this.flag = flag;
        this.group = group;
        this.playersList = playersList;
        setGroupMatches(groupMatches);
    }

    @NotNull
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @NotNull
    public File getFlag() {
        return flag;
    }

    public void setFlag(File flag) {
        this.flag = flag;
    }

    @Group
    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    @NotNull
    public List<String> getPlayersList() {
        return playersList;
    }

    public void setPlayersList(List<String> playersList) {
        this.playersList = playersList;
    }

    public List<Match> getGroupMatches() {
        return groupMatches;
    }

    public void setGroupMatches(List<Match> groupMatches) {
        this.groupMatches = groupMatches;

        if (groupMatches != null) {
            for (Match match : groupMatches) {
                String result = match.getResult();
                String[] goals = result.split(":");
                int scored = 0;
                int conceded = 0;

                if (match.getTeam1Name().equals(name)) {
                    scored = Integer.parseInt(goals[0]);
                    conceded = Integer.parseInt(goals[1]);
                } else if (match.getTeam2Name().equals(name)) {
                    scored = Integer.parseInt(goals[1]);
                    conceded = Integer.parseInt(goals[0]);
                } else {
                    continue;
                }

                goalsFor += scored;
                goalsAgainst += conceded;

                if (scored > conceded) {
                    points += 3;
                } else if (scored == conceded) {
                    points += 1;
                }
            }
        }
    }

    public int getPoints() {
        return points;
    }

    public int getGoalsFor() {
        return goalsFor;
    }

    public int getGoalsAgainst() {
        return goalsAgainst;
    }

    @Override
    public int compareTo(Team team) {
        if (points != team.points) {
            return points - team.points;
        }

        int goalDif = goalsFor - goalsAgainst;
        int otherGoalDif = team.goalsFor - team.goalsAgainst;

        if (goalDif != otherGoalDif) {
            return goalDif - otherGoalDif;
        }

        return goalsFor - team.goalsFor;
    }

}
